﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Contacts.Data.Data
{
   public class ContactsDataUsers
   {
      private string _appconfConnString = ConfigurationManager.AppSettings["DatabaseConnection"];

      public void SaveUser(string login, string password)
      {
         SqlConnection conn = new SqlConnection(_appconfConnString);
         conn.Open();

         SqlCommand comm = new SqlCommand("spAddUser", conn);
         comm.CommandType = CommandType.StoredProcedure;
         comm.Parameters.AddWithValue("@Login", Convert.ToString(login));
         comm.Parameters.AddWithValue("@Password", Convert.ToString(password));
         comm.ExecuteNonQuery();
         conn.Close();
      }

      public int UsersCount(string login, string password)
      {
         SqlConnection sqlConnectionToContactsDB = new SqlConnection(_appconfConnString);
         sqlConnectionToContactsDB.Open();

         SqlCommand cmd = new SqlCommand("spSelectUserLogin", sqlConnectionToContactsDB);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@Login", login);
         cmd.Parameters.AddWithValue("@Password", password);

         int usersCount = (int)cmd.ExecuteScalar();
         sqlConnectionToContactsDB.Close();

         return usersCount;
      }

   }
}
